using System;
using Light;
using UnityEngine;
using Weapons.Components.ComponentData;
using Weapons.Components.ComponentData.AttackData;

namespace Weapons.Components
{
    public class ActionHitBox : WeaponComponent<ActionHitBoxData, AttackActionHitBox>
    {
        [SerializeField] private Collider2D[] detecteds; //�洢������ײ�м�⵽��collision

        private CoreComp<Movement> movement;

        private Vector2 offset;

        protected override void Awake()
        {
            base.Awake();
        }

        protected override void Start()
        {
            base.Start();

            movement = new CoreComp<Movement>(Core);
            if (eventHandler != null) eventHandler.OnAttackAction += HandleAttackAction;
        }


        protected override void OnDestroy()
        {
            base.OnDestroy();
            eventHandler.OnAttackAction -= HandleAttackAction;
        }

        private void OnDrawGizmosSelected() //ע�⣺�������Ʒ������ڼ������ѡ�и������ʱ��Ż���Ч
        {
            if (data == null) return;

            foreach (var detected in data.AttackData)
            {
                if (!detected.Debug) continue;

                Gizmos.DrawWireCube(transform.position + (Vector3)detected.HitBox.center, detected.HitBox.size);
            }
        }

        public event Action<Collider2D[]> OnDetectedCollider2D;

        private void HandleAttackAction()
        {
            //��ǰ������������ҳ�治���ö���ϵͳ�������ײ���
            //�������ڼ�����ײ�е�����λ�ã�ע���ڱ���Ŀ��ͼƬ������λ�ڵײ�������֮����Ҫ������������ײ��һ��ĸ߶�
            offset.Set(
                transform.position.x + currentAttackData.HitBox.center.x * movement.Comp.FacingDirection,
                transform.position.y + currentAttackData.HitBox.center.y
            );

            detecteds = Physics2D.OverlapBoxAll(offset, currentAttackData.HitBox.size, 0f,
                data.DetectableLayers); //�ص�ѧϰ����
            //����Ĵ���ֻ�����������ʵ�ʹ���������ʵ�ʽӴ�������ײ�����ͣ�������Ϣ�㲥��ȥ���������ݽ���������������������
            //���������ĵ�һ���������ܵ��Ǿ��ε����ģ�����һ���ǣ������UnityAPI�ĵ�


            if (detecteds.Length == 0) return;

            OnDetectedCollider2D?.Invoke(detecteds);
        }
    }
}